﻿using FytSoa.Quartz;
using Microsoft.Extensions.DependencyInjection;
using Quartz.Impl.AdoJobStore;
using Quartz.Impl;
using Quartz;
using System;
using System.Collections.Specialized;
using FytSoa.Common.Utils;
using Quartz.Spi;

namespace FytSoa.CrossCutting
{
    public static class QuartzStrapperIoC
    {
        /// <summary>
        /// 任务持久化，基于数据库模式
        /// </summary>
        /// <param name="services"></param>
        public static void QuartzStartup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            NameValueCollection props = new()
            {
                { "quartz.serializer.type", "json" },
                //以下配置需要数据库表配合使用，表结构sql地址：https://github.com/quartznet/quartznet/tree/master/database/tables
                { "quartz.jobStore.type","Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"},
                { "quartz.jobStore.driverDelegateType",typeof(MySQLDelegate).AssemblyQualifiedName},
                { "quartz.jobStore.tablePrefix","QRTZ_"},
                { "quartz.jobStore.dataSource","myDS"},
                { "quartz.dataSource.myDS.connectionString", AppConfigUtils.MySqlConnectionString },
                { "quartz.dataSource.myDS.provider","MySql"},
                { "quartz.jobStore.useProperties","true"}
            };
            StdSchedulerFactory factory = new(props);
            services.AddSingleton<ISchedulerFactory>(factory);
            services.AddSingleton<IQuartzService, QuartzService>();
            services.AddTransient<HttpJob>();
        }
    }
}
